package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.ChanPinXiangQingDaoImpl;
import com.ysd.dao.impl.NewsXiangQingDaoImpl;
import com.ysd.dao.impl.XinWenDaoImpl;
import com.ysd.entity.XinWen;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminNewsChaXunServlet extends BaseServlet {
	/**
	 * 多条件查询新闻 localhost:8080/qiuxieshangcheng/adminNewsChaXun?reqName=addNews
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */

	public void getNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int xinWenId = 0;
		if (request.getParameter("xinWenId") != null && !request.getParameter("xinWenId").equals("")) {
			xinWenId = Integer.parseInt(request.getParameter("xinWenId"));
		}

		String xinWenName = request.getParameter("xinWenName");

		int xwWaiJianId = 0;
		if (request.getParameter("xwWaiJianId") != null && !request.getParameter("xwWaiJianId").equals("")) {
			xwWaiJianId = Integer.parseInt(request.getParameter("xwWaiJianId"));
		}
		String xinWenNeiRong = request.getParameter("xinWenNeiRong");

		String xinWenFebLieName = request.getParameter("xinWenFebLieName");
		XinWen xw = new XinWen(xinWenId, xinWenName, xwWaiJianId, xinWenNeiRong, xinWenFebLieName);

		XinWenDaoImpl xwDao = new XinWenDaoImpl();
		List<XinWen> xwList = xwDao.getXinWenByWhere(xw);

		Gson gson = new Gson();
		String res = gson.toJson(xwList);

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		out.print(res);

		out.flush();
		out.close();
	}

	/**
	 * 添加新闻 localhost:8080/qiuxieshangcheng/adminNewsChaXun?reqName=delXinWen
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */

	public void addNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		XinWenDaoImpl xwDao = new XinWenDaoImpl();// 新闻详情的实现类对象
		String xinWenName = request.getParameter("xinWenName");
		if (xinWenName != null && !xinWenName.equals("")) {
			List<XinWen> xwList = xwDao.getXinWenByName(xinWenName);
			if (xwList.size() == 0) {
				int xwWaiJianId = Integer.parseInt(request.getParameter("xwWaiJianId"));
				String xinWenNeiRong = request.getParameter("xinWenNeiRong");
				XinWen xws = new XinWen(0, xinWenName, xwWaiJianId, xinWenNeiRong, null);

				int n = xwDao.addNews(xws);
				if (n > 0) {
					out.print(Result.toClient(true, "添加成功", ""));
				} else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
			} else {
				out.print(Result.toClient(false, "新闻名称已存在", ""));
			}

		}
		out.flush();
		out.close();

	}

	// 删除新闻方法
	public void delXinWen(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int delxw = Integer.parseInt(request.getParameter("delxwId"));

		XinWenDaoImpl xw1Dao = new XinWenDaoImpl();// 创建实现类对象
		int n = xw1Dao.delXinWen(delxw);
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		if (n > 0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));
		}
		out.flush();
		out.close();
	}

	/**
	 * 添加新闻 localhost:8080/qiuxieshangcheng/adminNewsChaXun?reqName=getXWById
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */

	public void getXWById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		XinWenDaoImpl xwDao = new XinWenDaoImpl();// 新闻详情的实现类对象

		int Id = Integer.parseInt(request.getParameter("xwId"));
		XinWen xw = xwDao.getXWById(Id);

		Gson gson = new Gson();
		String res = gson.toJson(xw);

		out.print(res);

		out.flush();
		out.close();

	}

	/**
	 * 添加新闻 localhost:8080/qiuxieshangcheng/adminNewsChaXun?reqName=updateXW
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */

	public void updateXW(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		XinWenDaoImpl xwDao = new XinWenDaoImpl();// 创建实现类对象
		
		int xinWenId = Integer.parseInt(request.getParameter("xwId"));
		String xinWenName=request.getParameter("xwName");
		int xwWaiJianId = Integer.parseInt(request.getParameter("xwFenLei"));
		String xinWenNeiRong=request.getParameter("xwNeiRong");
		
		List<XinWen> xwList= xwDao.getXinWenByName(xinWenName);
		if (xwList.size()==0) {
			XinWen xw = new XinWen(xinWenId, xinWenName, xwWaiJianId, xinWenNeiRong, "");
			int n = xwDao.updateXW(xw);
			
			
					
			if (n > 0) {
				out.print(Result.toClient(true, "修改成功", ""));
			} else {
				out.print(Result.toClient(false, "修改失败", ""));
			}
		} else {
			out.print(Result.toClient(false, "新闻已存在", ""));
		}
		
		
		
		
		
		out.flush();
		out.close();
	}

}